package com.example.supplychainbackend.controller;

import com.example.supplychainbackend.controller.dto.DataResponse;
import com.example.supplychainbackend.controller.dto.blockchain.UploadInfo;
import com.example.supplychainbackend.controller.dto.enterprise.CertApplyProgressDto;
import com.example.supplychainbackend.controller.dto.enterprise.EnterpriseCertDto;
import com.example.supplychainbackend.controller.dto.enterprise.EnterpriseDto;
import com.example.supplychainbackend.controller.dto.user.UserSigDto;
import com.example.supplychainbackend.service.EnterpriseService;
import com.example.supplychainbackend.tool.JwtUtil;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/enterprise")
@AllArgsConstructor
public class EnterpriseController {

    private final EnterpriseService enterpriseService;

    @PostMapping("signup")
    public DataResponse<Long> createInfo(@RequestBody EnterpriseDto form) {
        Long infoId = enterpriseService.createEnterprise(form);
        return DataResponse.success(infoId);
    }

    @PutMapping("{id}")
    public DataResponse<EnterpriseDto> updateInfo(@PathVariable Long id, @RequestBody EnterpriseDto form) {
        EnterpriseDto res = enterpriseService.updateEnterprise(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/list")
    public DataResponse<List<EnterpriseDto>> getAllInfo() {
        List<EnterpriseDto> res = enterpriseService.listEnterprises();
        return DataResponse.success(res);
    }

    @GetMapping("{id}")
    public DataResponse<EnterpriseDto> getInfo(@PathVariable Long id) {
        EnterpriseDto res = enterpriseService.getEnterprise(id);
        return DataResponse.success(res);
    }

    @PostMapping("{id}/chain")
    public DataResponse<UploadInfo> uploadToChainCertificate(@PathVariable Long id) {
        UploadInfo txInfo = enterpriseService.uploadToChainCert(id);
        return DataResponse.success(txInfo);
    }

    @PostMapping("{id}/cert")
    public DataResponse<Long> applyCertificate(@RequestHeader("Authorization") String authHeader,
                                               @PathVariable Long id, @RequestBody EnterpriseCertDto applyForm) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        applyForm.setUserName(userSigDto.getUserName());
        applyForm.setUserId(userSigDto.getUserId());
        applyForm.setEnterpriseName(userSigDto.getEnterpriseName());
        applyForm.setEnterpriseId(userSigDto.getEnterpriseId());
        Long certEid = enterpriseService.applyEnterpriseCert(id,applyForm);
        return DataResponse.success(certEid);
    }

    @PutMapping("{id}/cert")
    public DataResponse<EnterpriseCertDto> updateCertificateApply(@RequestHeader("Authorization") String authHeader,
                                                                  @PathVariable Long id, @RequestBody EnterpriseCertDto applyForm) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        applyForm.setUserName(userSigDto.getUserName());
        applyForm.setUserId(userSigDto.getUserId());
        applyForm.setEnterpriseName(userSigDto.getEnterpriseName());
        applyForm.setEnterpriseId(userSigDto.getEnterpriseId());
        EnterpriseCertDto dto = enterpriseService.updateCertApply(id, applyForm);
        return DataResponse.success(dto);
    }

    @GetMapping("{id}/cert")
    public DataResponse<CertApplyProgressDto> getCertificateApplyProgress(@PathVariable Long id) {
        CertApplyProgressDto dto = enterpriseService.getCertApplyProgress(id);
        return DataResponse.success(dto);
    }

    @ExceptionHandler(Exception.class)
    public DataResponse<Exception> enterpriseErrorHandler(Exception e) {
        return DataResponse.error(500,e);
    }


}
